// pages/course/course.js
import { course } from '../../server/course';
import { file } from '../../server/file';
import { money } from '../../utils/money';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 课程列表
        courseList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        console.log('onshow');
        this.getCourseList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    // 进入课程详情页
    jmpCourseInfoPage(e) {
        // console.log(e.currentTarget.dataset);
        let { courseid } = e.currentTarget.dataset;
        wx.navigateTo({
            url: '/pages/courseInfo/courseInfo?course='+courseid
        });
    },

    jmpFindCoursePage() {
        wx.navigateTo({
            url: '/pages/findCourse/findCourse'
        });
    },

    async getCourseList() {
        let res = await course.getCourse();
        if (res.success) {
            let data = res.data.courseList;
            let l = [];
            try {
                for (let i = 0; i < data.length; i++) {
                    let imgUrl = await file.downloadPublicFile(data[i].photoId);
                    console.log(imgUrl);
                    let tmp = {
                        id: data[i].id,
                        img: imgUrl.data.url,
                        courseName: data[i].name,
                        price: money.num2yuan(data[i].price),
                        learnNum: data[i].learnNum
                    };
                    l.push(tmp);
                }
            } catch (err) {
                wx.showToast({
                    title: '请检查网络连接',
                    icon: 'none',
                    duration: 1000
                });
            }
            this.setData({ courseList: l });
        }
    },
    async loadMore() {
        let lastId = this.data.courseList[this.data.courseList.length - 1].id;
        let res = await course.getCourse(lastId);
        if (res.success) {
            let data = res.data.courseList;
            let courseList = this.data.courseList;
            try {
                for (let i = 0; i < data.length; i++) {
                    let imgUrl = await file.downloadPublicFile(data[i].photoId);
                    let tmp = {
                        id: data[i].id,
                        img: imgUrl.data.url,
                        courseName: data[i].name,
                        price: money.num2yuan(data[i].price),
                        learnNum: data[i].learnNum
                    };
                    courseList.push(tmp);
                }
            } catch (err) {
                wx.showToast({
                    title: '请检查网络连接',
                    icon: 'none',
                    duration: 1000
                });
            }
            this.setData({ courseList });
        }
    },
});